package com.tiny.mirror.service.product.domain.app;

import com.tiny.mirror.basic.db.domain.DomainBase;
import com.tiny.mirror.service.product.domain.DomainFactory;
import com.tiny.mirror.service.product.domain.platform.Platform;
import com.tiny.mirror.service.product.enums.AppStatusEnum;
import com.tiny.mirror.service.product.enums.AppTypeEnum;
import com.tiny.mirror.service.product.infrastructure.entity.db.AppEntity;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.javers.core.metamodel.annotation.Entity;
import org.javers.core.metamodel.annotation.Id;

/**
 * @Author: lanpai
 * @Description:
 * @Date: Create in 2023/9/24
 */
@Getter
@Setter
@Entity
public class App extends DomainBase<Long, AppEntity> implements Serializable {

  @Id
  private Long id = -1L;
  private String name;
  private BigDecimal price;
  private AppParams params;
  private AppTypeEnum type;
  private AppStatusEnum status;
  private Integer orderNum;
  private List<Platform> platformList;

  @Override
  public AppEntity toEntity() {
    return DomainFactory.convertToAppEntity(this);
  }

  @Override
  public void setIdAfterCreate(Long id) {
    this.id = id;
  }

  public void edit(App item) {
    if (Objects.isNull(item)) {
      return;
    }
    String name = item.getName();
    if (StringUtils.isNotEmpty(name)) {
      setName(name);
    }
    BigDecimal price = item.getPrice();
    if (price != null) {
      setPrice(price);
    }
    String previewImage = item.getParams().getPreviewImage();
    if (StringUtils.isNotBlank(previewImage)) {
      this.params.setPreviewImage(previewImage);
    }
    AppTypeEnum type = item.getType();
    if (type != null) {
      setType(type);
    }
    AppStatusEnum status = item.getStatus();
    if (status != null) {
      setStatus(status);
    }
    Integer orderNum = item.getOrderNum();
    if (orderNum != null) {
      setOrderNum(item.getOrderNum());
    }
  }

  public void deletePlatformList() {
    setPlatformList(null);
  }
}